package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.UserBean;
import com.easy.common.service.IUserService;
import com.easy.common.util.MonthGrowthVO;
import com.easy.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, UserBean> implements IUserService{
    @Autowired
    UserMapper userMapper;
    @Override
    public UserBean getUserById(Integer id) {
        return super.getById(id);
    }
    @Override
    public UserBean getUserByNickName(String nickname) {
        LambdaQueryWrapper<UserBean> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBean::getNickname, nickname);
        return super.getOne(queryWrapper);
    }
    @Override
    public int addUser(UserBean user) {
        if (super.save(user)) {
            return 1;
        }
        return 0;
    }

    @Override
    public int uploadUser(UserBean user) {
        return super.updateById(user)?1:0;
    }

    @Override
    public int deleteUser(Integer id) {
        return super.removeById(id)?1:0;
    }

    @Override
    public int reverseStatus(Integer id) {
        UserBean user = super.getById(id);
        if(user!=null){
            user.setIsdel(user.getIsdel()==1?0:1);
        }
        if(super.updateById(user)){
            return 1;
        }
        return 0;
    }

    @Override
    public List<UserBean> getAll(UserBean user) {
        LambdaQueryWrapper<UserBean> queryWrapper = new LambdaQueryWrapper<>();
        return super.list(queryWrapper);
    }

    @Override
    public IPage<UserBean> getPage(UserBean user, Page<UserBean> page) {
        LambdaQueryWrapper<UserBean> queryWrapper = new LambdaQueryWrapper<>();
        if(user!=null&& user.getRealname()!=null&& !user.getRealname().isEmpty()) {
            queryWrapper.like(UserBean::getRealname, user.getRealname());
        }
        if(user!=null&& user.getPhone()!=null&& !user.getPhone().isEmpty()) {
            queryWrapper.like(UserBean::getPhone, user.getPhone());
        }
        return super.page(page,queryWrapper);
    }

    @Override
    public List<MonthGrowthVO> getMonthlyUserGrowth() {
        return userMapper.countUserGrowthByMonth();
    }
}
